<?php session_start(); ?>
<?php 
    if(isset($_SESSION['name']))
        header('Location:admin.php');

?>
<html>
<head>
<link rel="stylesheet" href="style/style_login.css" type="text/css" />
<script language="javascript" src="lib/jquery-1.3.2.min.js"></script>
<script language="javascript">
    $(document).ready(function(){
      $('form').hide();     
      $('a').click(function(){
        $(this).parent('div').prev('form').slideToggle('normal');
        $(this).toggleClass('active');
        return false;
      }
      );
    }
    );

</script>
</head>
<body>
<?php

if(isset($_POST['submit'])){ 
  $errors=array();
  $required=array('email','pass');
      foreach ($required as $fieldname) {
        if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname]))
            $errors[]="The <strong>($fieldname) </strong> was left blank";
      } //End foreach
    
    if(empty($errors)){
        $conn=mysqli_connect('localhost','root','vertrigo','izwebz');
        $email=$_POST['email'];
        $pass=$_POST['pass'];
        $hass_pw=sha1($pass);
        $querry="SELECT CONCAT_WS(' ',first_name,last_name) 
                AS name
                FROM users 
                WHERE email='$email' 
                AND pass='$hass_pw'
                LIMIT 1
                 ";
        $result=mysqli_query($conn,$querry) or die (mysqli_error($conn));
      
      if(mysqli_num_rows($result)==1){ //Succsess Login
  
            while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
                $_SESSION['name']=$row['name'];
                header('Location:admin.php');
            }
        }else{
            $errors[]='The email or password does not match those in DB';
        }
        
    
    } //End empty($error)
        
}//End isset Submit
    else
    if(isset($_GET['start'])&&$_GET['start']==1) 
        $message="<ul><li>you are logged out!!</li></ul>";
?>

<div id="wrap">
    <?php
   
   if(!empty($errors)){
    
        echo "<ul>";
          foreach($errors as $error){
            echo "<li>$error </li>";
            }
        echo "</ul>";
    } 
    if(isset($message))
     echo $message;
    ?>
    <script language="javascript">
    function login()
    {
        $(form1).submit();
    }
    </script>
    <form action="" method="POST">
        <p>
        <label>Username:</label> 
        <input type="text" name="email"/>
        </p>
        <p>
        <label>Password:</label>
        <input type="text" name="pass"/>
        </p>
        <p>
        <input type="submit" name="submit" value="submit"/>
        </p>
    </form>
    
    <div class="login">
        <a href="#">Login</a>
    </div>
</div><!--end #wrap-->

</body>

</html>